<?php

/*

HTTPSEverywhere for Desktopd
Copyright (C) 2015  Desktopd Developer(s)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

https://www.gnu.org/licenses/agpl.html

*/


namespace Desktopd\HTTPSEverywhere;


class HTTPSEverywhereRuleset {
    protected $name = '';
    protected $platforms = array();
    protected $exclusions = array();
    protected $patterns = array();
    protected $downgradePatterns = array();
    
    protected $disabled = false;
    
    
    public function __construct ($name) {
        $this->name = "$name";
    }
    
    public function disable () {
        $this->disabled = true;
    }
    
    public function enable () {
        $this->disabled = false;
    }
    
    public function addPattern ($from, $to, $allowDowngrade = false) {
        $this->patterns[$from] = $to;
        if ($allowDowngrade) {
            $this->downgradePatterns[$from] = true;
        }
    }
    
    public function addExclusion ($pattern) {
        $this->exclusions[] = $pattern;
    }
    
    public function addPlatform ($platform) {
        if ($platform) {
            $this->platforms[] = $platform;
        }
    }
    
    public function platformSupported ($platform) {
        if (count($this->platforms) == 0) {
            return true; // not specified
        }
        return in_array($platform, $this->platforms, true);
    }
    
    protected function isHTTPS ($uri) {
        return preg_match('~^https://.+$~', $uri);
    }
    
    public function rewrite ($uri) {
        if ($this->disabled) return $uri;
        
        // NOTE: \xff is used as a delimiter
        foreach ($this->exclusions as $pattern) {
            if (!$pattern) continue;
            if (preg_match("\xff{$pattern}\xff", $uri)) {
                return $uri; // no rewrite
            }
        }
        
        foreach ($this->patterns as $from => $to) {
            if (!$from) continue;
            if (!preg_match("\xff{$from}\xff", $uri)) continue;
            
            $rewritten = preg_replace("\xff{$from}\xff", $to, $uri);
            
            if (!$this->isHTTPS($rewritten)) {
                // downgrade rewrite
                if (isset($this->downgradePatterns[$from])) {
                    // downgrade allowd
                    $uri = $rewritten;
                }
            } else {
                // rewrite to HTTPS
                $uri = $rewritten;
            }
        }
        
        return $uri;
    }
    
    public function getName () {
        return $this->name;
    }
}


// vim: ts=4 et ai

